﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Score
{
    public partial class PersonStudy_Branch : PortalPage
    {
        /// <summary>
        /// 每日之星 缓存key
        /// </summary>
        public const string PartyDayStarCacheKey = "PARTY_PartyScorePage_PartyDayStar_DATASOURCE";

        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return Request.QueryString["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return Request.QueryString["PageSize"] ?? "";
            }
        }
        /// <summary>
        /// SourceType
        /// </summary>
        private string ScoreType
        {
            get
            {
                return Request.QueryString["ScoreType"] ?? "";
            }
        }

        /// <summary>
        /// SourceType
        /// </summary>
        private string CountType
        {
            get
            {
                return Request.QueryString["CountType"] ?? "";
            }
        }

        /// <summary>
        /// Keyword
        /// </summary>
        private string Keyword
        {
            get
            {
                return Request.QueryString["Keyword"] ?? "";
            }
        }

        /// <summary>
        /// Keyword
        /// </summary>
        private string IsAllmember
        {
            get
            {
                return Request.QueryString["IsAllmember"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true);

            if (!string.IsNullOrWhiteSpace(PageIndex))
            {
                this.WriteBranchScoreList();
            }
            else {
                this.hfTotalCount.Value = PartyScoreHelper.QueryPartyMemberCount(this.Keyword,"",this.CurrentUser.BranchID, IsAllmember).ToString();
            }
                
            if (!IsPostBack)
            {
                this.PartyDayStar();
                if (!string.IsNullOrEmpty(this.CurrentUser.LoginID))
                {
                    this.divUserInfo.Visible = true;
                    this.BindUserScore();
                }
                else
                {
                    this.divUserInfo.Visible = false;
                }
            }
        }

        /// <summary>
        /// 绑定用户信息
        /// </summary>
        private void BindUserScore()
        {
            if (!string.IsNullOrEmpty(this.CurrentUser.LoginID))
            {
                PartyOrgMember om = DB.PartyOrgMembers.Find(this.CurrentUser.LoginID);

                this.lbUserName.Text = om.UserName;
                if (!string.IsNullOrEmpty(om.ImageUrl))
                    this.imgPhoto.ImageUrl = UpFileBusiness.GetFileUrl(om.ImageUrl);
                else
                    this.imgPhoto.ImageUrl = "../Resource/themes/default/images/Portal/party-none-photo.jpg";
                //用户积分
                DataTable dt = PartyScoreHelper.QueryMemberScore(this.CurrentUser.LoginID);

                if (dt != null && dt.Rows.Count > 0)
                {
                    this.lbScore.Text = Convert.ToString(dt.Rows[0]["Score"]);
                    this.lbClass.Text = PartyScoreHelper.GetStarClass(Convert.ToDecimal(Convert.ToString(dt.Rows[0]["Score"])), 25);
                }
            }
        }

        /// <summary>
        /// 党群每日之星
        /// </summary>
        private void PartyDayStar()
        {
            if (!CacheHelper.IsExist(PartyDayStarCacheKey))
            {
                CacheHelper.Add(PartyDayStarCacheKey, PartyScoreHelper.QueryDayStar(), DateTime.Now.AddHours(1));
            }
            DataTable dt = (DataTable)CacheHelper.GetCache(PartyDayStarCacheKey);

            if (dt != null && dt.Rows.Count > 0)
            {
                this.lbStarUserName.Text = Convert.ToString(dt.Rows[0]["UserName"]);
                this.lbStarClass.Text = PartyScoreHelper.GetStarClass(Convert.ToDecimal(dt.Rows[0]["Score"]), 25);
                if (!string.IsNullOrEmpty(dt.Rows[0]["Photo"].ToString()))
                    this.imgStarPhoto.ImageUrl = UpFileBusiness.GetFileUrl(Convert.ToString(dt.Rows[0]["Photo"]));
                else
                    this.imgStarPhoto.ImageUrl = "../Resource/themes/default/images/Portal/party-none-photo.jpg";
                this.lbStarScore.Text = Convert.ToString(dt.Rows[0]["Score"]);
            }
        }

        /// <summary>
        /// 个人学习排名
        /// </summary>
        private void WriteBranchScoreList()
        {
            StringBuilder sbHtml = new StringBuilder();
            
            DataTable dt = PartyScoreHelper.GetScoreRankMemberByBranchId(Convert.ToInt32(this.PageIndex) - 1, Convert.ToInt32(this.PageSize), this.CurrentUser.BranchID, this.Keyword).Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sbHtml.Append("<tr class=\"score-list-tr\">");
                sbHtml.AppendFormat("<td>{0}</td>", dr["Rank"]);
                sbHtml.AppendFormat("<td>{0}</td>", dr["UserName"]);
                sbHtml.AppendFormat("<td>{0}</td>", dr["BranchName"]);
                sbHtml.AppendFormat("<td><a href='{1}' target='_blank'> {0}</a></td>", dr["Score"], "PartyScoreDetail.aspx?Category=Member&LoginID=" + dr["LoginID"]);
                sbHtml.Append("</tr>");
            }

            this.WriteString(sbHtml.ToString());
        }

        /// <summary>
        /// 写流
        /// </summary>
        /// <param name="str"></param>
        private void WriteString(string str)
        {
            Response.Clear();
            Response.Write(str);
            Response.End();
        }
    }
}